package com.game.jumper.activity;

import com.example.jumperball.R;

import android.R.integer;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.media.AudioManager;
import android.media.SoundPool;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.SurfaceHolder.Callback;

/**
 * 
 * @author Himi
 *
 */
public class MySurfaceView extends SurfaceView implements Callback, Runnable {
	private SurfaceHolder sfh;
	private Paint paint;
	private Thread th;
	private boolean flag;
	private Canvas canvas;
	private int screenW, screenH;
	private int index=0;
	private Bitmap[] bitmaps=new Bitmap[4];
	private Bitmap ball;
	private int[] indexs=new int[]{0,1,2,3,2,1};
	/**
	 * SurfaceView��ʼ������
	 */
		public MySurfaceView(Context context) {
			super(context);
			sfh = this.getHolder();
			sfh.addCallback(this);
			paint = new Paint();
			paint.setColor(Color.BLACK);
			paint.setAntiAlias(true);
			setFocusable(true);
			
			bitmaps[0]=BitmapFactory.decodeResource(this.getResources(), R.drawable.welcome1);
			bitmaps[1]=BitmapFactory.decodeResource(this.getResources(), R.drawable.welcome2);
			bitmaps[2]=BitmapFactory.decodeResource(this.getResources(), R.drawable.welcome3);
			bitmaps[3]=BitmapFactory.decodeResource(this.getResources(), R.drawable.welcome4);
			ball=BitmapFactory.decodeResource(this.getResources(), R.drawable.ball01);
		}

	/**
	 * SurfaceView��ͼ��������Ӧ�˺���
	 */
	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		screenW = this.getWidth();
		screenH = this.getHeight();
		flag = true;
		//ʵ���߳�
		th = new Thread(this);
		//�����߳�
		th.start();
		//this.setBackgroundColor(Color.BLACK);
	}

	/**
	 * ��Ϸ��ͼ
	 */
	public void myDraw() {
		try {
			canvas = sfh.lockCanvas();
			if (canvas != null) {
				Bitmap image = bitmaps[indexs[index]];
				
				canvas.drawBitmap(image, new Rect(0, 0, image.getWidth(), image.getHeight()),
				new Rect(0, 0, screenW, screenH),paint);
				canvas.drawBitmap(ball, new Rect(0, 0, ball.getWidth(), ball.getHeight()),
						new Rect(
								(int)(0)
								, (int)((screenH-screenW)*0.8)
								, (int)(screenW)
								, (int)((screenH-screenW)*0.8+screenW*0.85)
								),paint);
				index++;
				
				index%=5;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			if (canvas != null)
				sfh.unlockCanvasAndPost(canvas);
		}
	}

	/**
	 * �����¼�����
	 */
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return false;
	}

	@Override
	public void run() {
		while (flag) {
			long start = System.currentTimeMillis();
			myDraw();
			long end = System.currentTimeMillis();
			try {
				if (end - start < 200) {
					Thread.sleep(200 - (end - start));
				}
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * SurfaceView��ͼ״̬����ı䣬��Ӧ�˺���
	 */
	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
	}

	/**
	 * SurfaceView��ͼ����ʱ����Ӧ�˺���
	 */
	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		flag = false;
		for (Bitmap element : bitmaps) {
			if(element==null)
				continue;
			if(element.isRecycled()==false) //如果没有回收  
				element.recycle();   
		}
	}
	
}
